package com.oro.backingbeans;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;
import org.primefaces.component.password.Password;
import org.springframework.beans.factory.annotation.Autowired;

import com.oro.domain.Authority;
import com.oro.domain.User;
import com.oro.service.UserService;

public class InserisciUtenteBean {

	Password confirmPassword;

	private Logger logger;
	Password password;

	private List<SelectItem> selectUserTypeOptions;

	private User userInput;

	private User userOutput;
	@Autowired
	private UserService userService;

	private String userType;

	public InserisciUtenteBean() {
		super();
		userInput = new User();
		userOutput = new User();

		selectUserTypeOptions = new ArrayList<SelectItem>();
		selectUserTypeOptions.add(new SelectItem("NO_INPUT", "        "));
		selectUserTypeOptions.add(new SelectItem(Authority.ROLE_OPERATOR,
				"Operator"));
		selectUserTypeOptions
				.add(new SelectItem(Authority.ROLE_ADMIN, "Admin"));
		selectUserTypeOptions.add(new SelectItem(Authority.ROLE_USER, "User"));
		selectUserTypeOptions.add(new SelectItem(Authority.ROLE_STUDENT,
				"Student"));
	}

	public Password getConfirmPassword() {
		return confirmPassword;
	}

	private Logger getLogger() {
		if (logger == null) {
			logger = Logger.getLogger(InserisciUtenteBean.class);
		}
		return logger;
	}

	public Password getPassword() {
		return password;
	}

	public List<SelectItem> getSelectUserTypeOptions() {
		return selectUserTypeOptions;
	}

	public User getUserInput() {
		return userInput;
	}

	public User getUserOutput() {
		return userOutput;
	}

	public UserService getUserService() {
		return userService;
	}

	public String getUserType() {
		return userType;
	}

	public String inserisciUtente(User user) {
		userService.save(user);
		return "inserisciUtenteSuccess";
	}

	@PostConstruct
	public void postConstruct() {
		getLogger().info("InserisciUtenteBean has been constructed.");
	}

	@PreDestroy
	public void preDestroy() {
		getLogger().info("InserisciUtenteBean is being destroyed.");
	}

	public void setConfirmPassword(Password confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public void setPassword(Password password) {
		this.password = password;
	}

	public void setSelectUserTypeOptions(List<SelectItem> selectUserTypeOptions) {
		this.selectUserTypeOptions = selectUserTypeOptions;
	}

	public void setUserInput(User userInput) {
		this.userInput = userInput;
	}

	public void setUserOutput(User userOutput) {
		this.userOutput = userOutput;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String submit() {
		// Prima vediamo se le due password sono uguali
		if (!userInput.getPassword().equals(userInput.getConfirmPassword())) {

			FacesContext context = FacesContext.getCurrentInstance();

			String baseName = context.getApplication().getMessageBundle();
			Locale currentLocale = context.getViewRoot().getLocale();
			ResourceBundle rb = ResourceBundle.getBundle(baseName,
					currentLocale);

			FacesMessage message = new FacesMessage();
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			message.setSummary(rb.getString("passwords_dont_match"));

			context.addMessage(password.getClientId(context), message);

			return "";

		}
		userInput.addAuthority(userType);
		userService.save(userInput);
		userOutput = userInput;
		userInput = new User();
		userType = null;
		return "inserisciUtenteSuccess?faces-redirect=true";
	}

}
